package com.itcm.purchase.controller;

import com.itcm.purchase.common.R;
import com.itcm.purchase.common.ResultCodeEnum;
import com.itcm.purchase.entity.dto.LoginUser;
import com.itcm.purchase.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author sky
 * @Site cmtianxie163.com 2020/6/18 10:13
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
@Api(tags = "用户登录")
public class SysUserController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody LoginUser loginUser) {
        String token = sysUserService.login(loginUser.getUsername(), loginUser.getPassword());
        if (token == null) {
            return R.setResult(ResultCodeEnum.VALIDATE_FAILED).message("用户名密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("tokenHeader", tokenHeader);
        tokenMap.put("token", tokenHead + ' ' + token);
        return R.ok().data("data", tokenMap);
    }
}
